package com.woniuxy.credit_loans.accounting_shi.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.credit_loans.accounting_shi.dto.LiuDto;
import com.woniuxy.credit_loans.accounting_shi.service.BankService;
import com.woniuxy.credit_loans.accounting_shi.service.PipelineService;
import com.woniuxy.credit_loans.entity.BankTable;
import com.woniuxy.credit_loans.entity.Pipeline;
import com.woniuxy.credit_loans.util.ObjectExcelRead;
import com.woniuxy.credit_loans.util.ObjectExcelView;
import com.woniuxy.credit_loans.util.PageData;
import com.woniuxy.credit_loans.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.nio.channels.Pipe;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/banks")
public class BankController {
    @Autowired
    private BankService bankService;
    @Autowired
    private PipelineService pipelineService;
    @GetMapping("")
    public Result b1(){
        List<BankTable> bankTables = bankService.selectAll();
        return Result.ok().data("list",bankTables);
    }
    @PostMapping("/pis")
    public Result p1(@RequestBody LiuDto liuDto){
        PageHelper.startPage(liuDto.getCurrentPage(), liuDto.getPageSize(),true);
        List<Pipeline> pipelines = pipelineService.selectAll();
        PageInfo pageInfo=new PageInfo(pipelines);
        long total = pageInfo.getTotal();
        return Result.ok().data("list",pipelines).data("total",total);
    }
    @RequestMapping(value = "/exportExcel")
    public ModelAndView exportExcel(@RequestBody LiuDto user) {
        ModelAndView mv = new ModelAndView();
        try {
            Map<String, Object> dataMap = new HashMap<>();
            //构造列名称
            List<String> titles = new ArrayList<>();
            titles.add("流水编号"); // 1
            titles.add("银行账号"); // 2
            titles.add("类型"); // 3
            titles.add("金额"); // 4
            dataMap.put("titles", titles);
            //得到被导出的数据
            List<Pipeline> varOList = pipelineService.selectAll();
            List<PageData> varList = new ArrayList<>();
            for (Pipeline userVO : varOList) {
                PageData vpd = new PageData();
                vpd.put("var1",userVO.getPipelineId());
                vpd.put("var2",userVO.getPipelineBy1());
                vpd.put("var3",userVO.getPipelineInorout());
                vpd.put("var4",userVO.getPipelineBy2());
                varList.add(vpd);
            }
            dataMap.put("varList", varList);
            ObjectExcelView erv = new ObjectExcelView();
            mv = new ModelAndView(erv, dataMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }
    @RequestMapping(value="/importExcel")
    public Result importExcel(MultipartFile file, HttpServletRequest request) throws Exception{

        if (null != file && !file.isEmpty()) {
            //执行读EXCEL操作,读出的数据导入List 2:从第2行开始；0:从第A列开始；0:第0个sheet
            List<PageData> listPd = (List) ObjectExcelRead.readExcel(file.getInputStream(), 1, 0, 0);
            for (PageData pageData : listPd) {
                Pipeline pipeline=new Pipeline();
                pipeline.setPipelineBy1(pageData.getString("var0"));
                pipeline.setPipelineType(pageData.getString("var1"));
                pipeline.setPipelineInorout(pageData.getString("var2"));
                pipeline.setPipelineBy2(pageData.getString("var3"));
                pipelineService.insertSelective(pipeline);
            }

        }
        return Result.ok("导入成功");
    }
    @GetMapping("{id}")
    public Result b2(@PathVariable int id){
        BankTable bankTable = bankService.selectByPrimaryKey(id);
        return Result.ok().data("bank",bankTable);
    }
    @PostMapping("")
    public Result b3(@RequestBody BankTable table){
        bankService.insertSelective(table);
        return Result.ok();
    }
    @PutMapping("")
    public Result b4(@RequestBody BankTable table){
        bankService.updateByPrimaryKeySelective(table);
        return  Result.ok();
    }
    @DeleteMapping("{id}")
    public Result b5(@PathVariable Long id){
        bankService.deleteByPrimaryKey(id);
        return Result.ok();
    }
}
